﻿'use strict';

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();

// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
jQuery(document).ready(function ($) {

    var iFrame = $("#SPAppIFrameWE");

    //getUserName();
    $('#worksheets-manage').modal({
        "backdrop": "static",
        "show": false
    });

    ListHelper.GetList();

    //Tab Clicked
    $('#worksheets-items').on("click", "a", function (e) {
        e.preventDefault();
        iFrame.prop("src", $(this).prop("href"));
    });

    //Table Remove Clicked
    $('#worksheets-table').on("click", "a", function (e) {
        e.preventDefault();
        var href = $(this).prop("href");
        var hrefSplit = href.split("#;");
        var id = hrefSplit[1];
        var title = hrefSplit[2];
        $(this).closest('tr').remove();

        ListHelper.Delete(id);
    });

    //On Add Clicked
    $('#worksheets-btn-add').on("click", function (e) {
        e.preventDefault();
        var title = $('#txtTitle').val();
        if (title) {
            $('#txtTitle').closest('.form-group').removeClass('has-error');
        }
        else {
            $('#txtTitle').closest('.form-group').addClass('has-error');
        }

        var url = $('#txtUrl').val();
        if (url) {
            $('#txtUrl').closest('.form-group').removeClass('has-error');
        }
        else {
            $('#txtUrl').closest('.form-group').addClass('has-error');
        }
        //Check if title already exist
        if (!isTitleExist(title)) {
            if (title && url) {
                AddNewRowUI(0, title, url);
                ListHelper.Add(title, url);
            }
        }
        else {
            $('#txtTitle').closest('.form-group').addClass('has-error');
        }
    });

    /*iFrame.on("load", function () {
        // resize logic;

        var DOM = iFrame[0],
            cW = $(DOM.contentDocument);

        iFrame.css({
            width: "100%",
            height: cW.height() || "auto"
        });
    });*/
});

function isTitleExist(title) {
    var result = false;
    $('#worksheets-table td.we-title').each(function (index) {
        var colVal = $(this).text();
        if (colVal == title) {
            result = true; return result;
        }
    });
    return result;
}

function AddNewRowUI(id, title, url) {
    var rowCount = $('#worksheets-table tr').length;
    $('#worksheets-table tr:last').after('<tr><td>' + rowCount + '</td><td class="we-title">' + title + '</td><td class="we-title">' + url + '</td><td><a href="'  + '#;' + id + '#;' + title + '"><span class="glyphicon glyphicon-remove-circle"></span></a></td></tr>');
    $('#txtTitle').val('');
    $('#txtUrl').val('');
}

// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName() {
    context.load(user);
    context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}

// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function onGetUserNameSuccess() {
    $('#message').text('Hello ' + user.get_title());
}

// This function is executed if the above call fails
function onGetUserNameFail(sender, args) {
    alert('Failed to get user name. Error:' + args.get_message());
}
